package com.br.ufrn.laut.braipetro.app.malha;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.br.ufrn.laut.braipetro.app.ConfiguracaoActivity;
import com.br.ufrn.laut.braipetro.app.R;
import com.br.ufrn.laut.braipetro.app.conexao.ComunicacaoJSON;


public class MalhaActivity extends Activity {

    private ListView list;
    protected TextView txvId;
    protected TextView txvNome;
    //TextView api;
    private Button btnDados;
    private ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

    //URL to get JSON Array
    private String url = ConfiguracaoActivity.servidor + "/datasnap/rest/tsmtmalha/getMalhas";

    //JSON Node Names
    private static final String TAG_ID_MALHA = "Id";
    private static final String TAG_NOME_MALHA = "Nome";

    JSONArray jsonArrayDadosMalha = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.malha_activity);
        oslist = new ArrayList<HashMap<String, String>>();

        btnDados = (Button)findViewById(R.id.btnDados);
        btnDados.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                new JSONParse().execute();
            }
        });
    }

    private class JSONParse extends AsyncTask<String, String, JSONArray> {
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            txvId = (TextView)findViewById(R.id.id);
            txvNome = (TextView)findViewById(R.id.nome);
            //api = (TextView)findViewById(R.id.api);
            pDialog = new ProgressDialog(MalhaActivity.this);
            pDialog.setMessage("Recebendo Dados ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected JSONArray doInBackground(String... args) {

            ComunicacaoJSON jParser = new ComunicacaoJSON();

            // Getting JSON from URL
            JSONArray json = jParser.getJSON(url);
            return json;
        }
        @Override
        protected void onPostExecute(JSONArray json) {
            pDialog.dismiss();
            try {
                // Getting JSON Array from URL
                jsonArrayDadosMalha = json;
                for(int i = 0; i < jsonArrayDadosMalha.length(); i++){
                    JSONObject c = jsonArrayDadosMalha.getJSONObject(i);

                    // Storing  JSON item in a Variable
                    String id = c.getString(TAG_ID_MALHA);
                    String nome = c.getString(TAG_NOME_MALHA);
                    //String api = c.getString(TAG_API);

                    // Adding value HashMap key => value
                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_ID_MALHA, id);
                    map.put(TAG_NOME_MALHA, nome);
                    //map.put(TAG_API, api);

                    oslist.add(map);
                    list=(ListView)findViewById(R.id.list);

                    ListAdapter adapter = new SimpleAdapter(MalhaActivity.this, oslist,
                            R.layout.listitens,
                            new String[] { TAG_ID_MALHA,TAG_NOME_MALHA }, new int[] {
                            R.id.id,R.id.nome});

                    list.setAdapter(adapter);
                    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            Toast.makeText(MalhaActivity.this, "Você clicou na malha "
                                    +oslist.get(+position).get("nome"), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}